﻿/// Write a method that adds two polynomials. Represent them as arrays of their coefficients as in the example below:
///		x2 + 5 = 1x2 + 0x + 5 
/// Extend the program to support also subtraction and multiplication of polynomials.

/// Thanks to -> http://forums.academy.telerik.com/user/%D0%98%D0%B2%D0%B0%D0%B9%D0%BB%D0%BE+%D0%9A%D0%B5%D0%BD%D0%BE%D0%B2
using System;

class zad11_12
{

    static void Main()
    {
        decimal[] firstPolinomial = { 5, -1 };
        Console.WriteLine("First polinomial:");
        PrintPolinomial(firstPolinomial);
        Console.WriteLine();

        decimal[] secondPolinomial = { 10, -5, 6 };
        Console.WriteLine("Second polinomial:");
        PrintPolinomial(secondPolinomial);
        Console.WriteLine();

        int maxLenght = 0;
        if (firstPolinomial.Length > secondPolinomial.Length)
        {
            maxLenght = firstPolinomial.Length;
        }
        else
        {
            maxLenght = secondPolinomial.Length;
        }

        decimal[] result = new decimal[maxLenght];
        Console.WriteLine();

        SumOfPolinomials(firstPolinomial, secondPolinomial, result);

        Console.WriteLine("Sum:");
        PrintPolinomial(result);
        Console.WriteLine();

        SubstractionOfPolinomials(firstPolinomial, secondPolinomial, result);

        Console.WriteLine("Substraction:");
        PrintPolinomial(result);
        Console.WriteLine();

        decimal[] multiply = new decimal[firstPolinomial.Length + secondPolinomial.Length];

        MultiplyPolinomials(firstPolinomial, secondPolinomial, multiply);

        Console.WriteLine("Multiplication:");
        PrintPolinomial(multiply);
        Console.WriteLine();
    }
    

    static void SumOfPolinomials(decimal[] firstPolinomial, decimal[] secondPolinomial, decimal[] result)
    {
        int minLenght = 0;
        int smallerPolinomial = 0;

        if (firstPolinomial.Length > secondPolinomial.Length)
        {
            minLenght = secondPolinomial.Length;
            smallerPolinomial = 2;
        }
        else
        {
            minLenght = firstPolinomial.Length;
            smallerPolinomial = 1;
        }

        for (int i = 0; i < minLenght; i++)
        {
            result[i] = firstPolinomial[i] + secondPolinomial[i];
        }

        for (int i = minLenght; i < result.Length; i++)
        {
            if (smallerPolinomial == 1)
            {
                result[i] = secondPolinomial[i];
            }
            else
            {
                result[i] = firstPolinomial[i];
            }
        }
    }


    static void SubstractionOfPolinomials(decimal[] firstPolinomial, decimal[] secondPolinomial, decimal[] result)
    {
        int minLenght = 0;
        int smallerPolinomial = 0;

        if (firstPolinomial.Length > secondPolinomial.Length)
        {
            minLenght = secondPolinomial.Length;
            smallerPolinomial = 2;
        }
        else
        {
            minLenght = firstPolinomial.Length;
            smallerPolinomial = 1;
        }

        for (int i = 0; i < minLenght; i++)
        {
            result[i] = firstPolinomial[i] - secondPolinomial[i];
        }

        for (int i = minLenght; i < result.Length; i++)
        {
            if (smallerPolinomial == 1)
            {
                result[i] = secondPolinomial[i];
            }
            else
            {
                result[i] = firstPolinomial[i];
            }
        }
    }

    static void MultiplyPolinomials(decimal[] firstPolinomial, decimal[] secondPolinomial, decimal[] result)
    {

        for (int i = 0; i < result.Length; i++)
        {
            result[i] = 0;
        }

        for (int i = 0; i < firstPolinomial.Length; i++)
        {
            for (int j = 0; j < secondPolinomial.Length; j++)
            {
                int position = i + j;
                result[position] += (firstPolinomial[i] * secondPolinomial[j]);
            }
        }
    }

    static void PrintPolinomial(decimal[] polinomial)
    {
        for (int i = polinomial.Length - 1; i >= 0; i--)
        {
            if (polinomial[i] != 0 && i != 0)
            {
                if (polinomial[i - 1] >= 0)
                {
                    Console.Write("{1}x^{0} +", i, polinomial[i]);
                }
                else
                {
                    Console.Write("{1}x^{0} ", i, polinomial[i]);
                }
            }
            
            if (i == 0)
            {
                Console.Write("{0}", polinomial[i]);
            }
        }

        Console.WriteLine();
    }
}